let type = "test";
let BASE_URL = "";

// 绽放的
if (type == "test") {
	BASE_URL = 'https://api.zhanfang.comdns.cn/uniapp';
}

if (type == "pro") {
	BASE_URL = 'https://api.zhanfang.comdns.cn/uniapp';
}

// 美模的
// if (type == "test") {
// 	BASE_URL = 'https://api.meimo.comdns.cn/uniapp';
// }

// if (type == "pro") {
// 	BASE_URL = 'https://api.meimo.comdns.cn/uniapp';
// }



function randomString(len) {
	len = len || 32;
	const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
	const maxPos = $chars.length;
	let pwd = '';
	let i;
	for (i = 0; i < len; i++) {
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
}


function request(options) {
	return new Promise((resolve, reject) => {
		// 获取用户令牌
		let token = uni.getStorageSync('token')
		// 设置请求头
		const header = {
			'Content-Type': 'application/json',
			Authorization: token,
			...options.header // 可以传入额外的请求头参数
		}
		uni.request({
			url: BASE_URL + options.url, //接收请求的API
			method: options.method || 'POST', //接收请求的方式,如果不传默认为GET
			data: options.data || {}, //接收请求的data,不传默认为空
			header: header, //接收请求的header
			timeout: 10000,
			success: ({
				data,
				statusCode
			}) => {
				console.log('data', data)
				console.log('statusCode', statusCode)
				if (data.code == 200) {
					resolve(data);
				} else if (data.code == 401) {
					// 获取当前页路由
					var pages = getCurrentPages();
					let currentRoute = pages[pages.length - 1].route;
					if (currentRoute != "pages/login/login") {
						uni.showToast({
							title: data.message,
							icon: 'none',
							mask: true,
							duration: 1000
						})
						setTimeout(function() {
							LogoutApi().then(res => {
								uni.removeStorageSync("isLogin");
								uni.removeStorageSync("token");
								uni.navigateTo({
									url: '/pages/login/login'
								})
							})
						}, 1000)
					}
				} else {
					uni.showToast({
						title: data.message,
						icon: 'none',
						mask: true,
						duration: 3000
					})
				}
			},
			fail: (error) => {
				console.log('error', error)
				uni.getNetworkType({
					complete: e => {
						let networkType = e.networkType || 'none';
						if (networkType == "none") {
							plus.nativeUI.toast("当前无网络，请检查您的网络链接");
						}
					}
				});
				reject(error);
			}
		});
	});
}

export default request;